﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class represents a mathematical complex vector with n components.
    /// </summary>
    [Serializable]
    public class GeneralComplexVector : AbstractGeneralVector, IGeneralComplexVector
    {
        /// <summary>
        /// Saves the components of the complex vector.
        /// </summary>
        private ComplexNumber[] vectorData;

        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralComplexVector"/> class.
        /// </summary>
        /// <param name="vectorData">The GeneralComplexVector to clone.</param>
        public GeneralComplexVector(GeneralComplexVector vectorData)
        {
            if (vectorData == (GeneralComplexVector) null)
            {
                throw new ArgumentNullException("vectorData");
            }

            this.VectorData = (ComplexNumber[]) (vectorData.VectorData.Clone());
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralComplexVector"/> class.
        /// </summary>
        /// <param name="data">The components of the complex vector.</param>
        public GeneralComplexVector(ComplexNumber[] data)
        {
            this.VectorData = data;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GeneralComplexVector"/> class.
        /// </summary>
        /// <param name="dimension">The dimension of the complex vector.</param>
        public GeneralComplexVector(int dimension)
        {
            if (dimension < 0)
            {
                throw new ArgumentException("dimension < 0", "dimension");
            }

            this.vectorData = new ComplexNumber[dimension];

            for (int i = 0; i < dimension; i++)
            {
                this.vectorData[i] = new ComplexNumber();
            }
        }

        /// <summary>
        /// Gets or sets the components of the complex vector.
        /// </summary>
        /// <value>The components of the complex vector.</value>
        public ComplexNumber[] VectorData
        {
            get { return vectorData; }
            set { vectorData = value; }
        }

        /// <summary>
        /// Counts the components of the complex vector.
        /// </summary>
        /// <value>The components of the complex vector.</value>
        public int Count
        {
            get { return vectorData.Length; }
        }

        /// <summary>
        /// Gets or sets the <see cref="SmartMathLibrary.ComplexNumber"/> at the specified index.
        /// </summary>
        /// <value>The complex component of the vector at the specified position.</value>
        public ComplexNumber this[int index]
        {
            get { return this.GetComponent(index); }
            set { this.SetComponent(index, value); }
        }

        /// <summary>
        /// Extracts the real number part of the complex number values into a new vector.
        /// </summary>
        /// <returns>The real number part of the complex number values.</returns>
        public GeneralVector ExtractRealNumberPartVector()
        {
            GeneralVector result = new GeneralVector(this.Count);

            for (int i = 0; i < this.Count; i++)
            {
                result[i] = this[i].RealNumberPart;
            }

            return result;
        }

        /// <summary>
        /// Extracts the imaginary number part of the complex number values into a new vector.
        /// </summary>
        /// <returns>The imaginary number part of the complex number values.</returns>
        public GeneralVector ExtractImaginaryNumberPartVector()
        {
            GeneralVector result = new GeneralVector(this.Count);

            for (int i = 0; i < this.Count; i++)
            {
                result[i] = this[i].ImaginaryNumberPart;
            }

            return result;
        }

        /// <summary>
        /// Gets the component value.
        /// </summary>
        /// <param name="index">The index of the component.</param>
        /// <returns>The vector component value at the over given index</returns>
        public ComplexNumber GetComponent(int index)
        {
            return this.vectorData[index];
        }

        /// <summary>
        /// Sets the component value at a specified position.
        /// </summary>
        /// <param name="index">The index of the component.</param>
        /// <param name="value">The new value of the component.</param>
        public void SetComponent(int index, ComplexNumber value)
        {
            this.vectorData[index] = value;
        }

        /// <summary>
        /// Counts the components of the vector.
        /// </summary>
        /// <value>The number of components of the vector.</value>
        public GeneralComplexVector Copy()
        {
            GeneralComplexVector result = new GeneralComplexVector(this.Count);

            for (int i = 0; i < this.vectorData.Length; i++)
            {
                result[i] = this[i].Copy();
            }

            return result;
        }

        /// <summary>
        /// Converts the current instance of the vector to a row matrix.
        /// </summary>
        /// <returns>The converted matrix.</returns>
        public ComplexMatrix ToMatrix()
        {
            ComplexMatrix matrix = new ComplexMatrix(this.vectorData.Length, 1);

            for (int i = 0; i < this.vectorData.Length; i++)
            {
                matrix.SetValueAtPosition(new MatrixPosition(i, 0), this.vectorData[i]);
            }

            return matrix;
        }

        /// <summary>
        /// Converts the data of the vector to an array of complex numbers.
        /// </summary>
        /// <returns>An array of complex numbers.</returns>
        public ComplexNumber[] ToArray()
        {
            return (ComplexNumber[]) this.vectorData.Clone();
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first complex vector.</param>
        /// <param name="b">The second complex vector.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(GeneralComplexVector a, GeneralComplexVector b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            if (a.Count != b.Count)
            {
                return false;
            }

            for (int i = 0; i < a.Count; i++)
            {
                if (a.GetComponent(i) != b.GetComponent(i))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first complex vector.</param>
        /// <param name="b">The second complex vector.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(GeneralComplexVector a, GeneralComplexVector b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Unions the specified two complex vectors.
        /// </summary>
        /// <param name="a">The first complex vector to union.</param>
        /// <param name="b">The second complex vector to union.</param>
        /// <returns>The two source vector values in one vector.</returns>
        public static GeneralComplexVector UnionVectors(GeneralComplexVector a, GeneralComplexVector b)
        {
            if (a == (GeneralComplexVector) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (GeneralComplexVector) null)
            {
                throw new ArgumentNullException("b");
            }

            GeneralComplexVector result = new GeneralComplexVector(a.Count + b.Count);

            for (int i = 0; i < a.Count; i++)
            {
                result[i] = a[i];
            }

            int add = a.Count;

            for (int i = 0; i < b.Count; i++)
            {
                result[i + add] = b[i];
            }

            return result;
        }

        /// <summary>
        /// Implements the operator +.
        /// </summary>
        /// <param name="a">The first vector.</param>
        /// <param name="b">The second vector.</param>
        /// <returns>The result of the operator.</returns>
        public static GeneralComplexVector operator +(GeneralComplexVector a, GeneralComplexVector b)
        {
            if (a == (GeneralComplexVector) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (GeneralComplexVector) null)
            {
                throw new ArgumentNullException("b");
            }

            if (a.Count != b.Count)
            {
                throw new ArithmeticException("Number of components in both vectors have to be even.");
            }

            ComplexNumber[] result = new ComplexNumber[a.Count];

            for (int i = 0; i < a.Count; i++)
            {
                result[i] = a.GetComponent(i) + b.GetComponent(i);
            }

            return new GeneralComplexVector(result);
        }

        /// <summary>
        /// Implements the operator -.
        /// </summary>
        /// <param name="a">The first vector.</param>
        /// <param name="b">The second vector.</param>
        /// <returns>The result of the operator.</returns>
        public static GeneralComplexVector operator -(GeneralComplexVector a, GeneralComplexVector b)
        {
            if (a == (GeneralComplexVector) null)
            {
                throw new ArgumentNullException("a");
            }

            if (b == (GeneralComplexVector) null)
            {
                throw new ArgumentNullException("b");
            }

            if (a.Count != b.Count)
            {
                throw new ArithmeticException("Number of components in both vectors have to be even.");
            }

            ComplexNumber[] result = new ComplexNumber[a.Count];

            for (int i = 0; i < a.Count; i++)
            {
                result[i] = a.GetComponent(i) - b.GetComponent(i);
            }

            return new GeneralComplexVector(result);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            String result = "";

            for (int i = 0; i < this.vectorData.Length; i++)
            {
                result += this.vectorData[i] + "; ";
            }

            if (result.Length > 2)
            {
                result = result.Substring(0, result.Length - 2);
            }

            return result;
        }
    }
}